package cn.tedu.csmall.passport.controller;

import cn.tedu.csmall.passport.pojo.dto.AdminAddNewDTO;
import cn.tedu.csmall.passport.pojo.dto.AdminLoginDTO;
import cn.tedu.csmall.passport.pojo.vo.AdminListItemVO;
import cn.tedu.csmall.passport.security.LoginPrincipal;
import cn.tedu.csmall.passport.service.IAdminService;
import cn.tedu.csmall.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "01管理员模块")
@RestController
@RequestMapping("/admins")
@Slf4j
@Validated
public class AdminController {

    @Autowired
    private IAdminService iAdminService;

    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 50)
    @PostMapping("login")
    public JsonResult<String> login(AdminLoginDTO adminLoginDTO){
       log.debug("开始处理{管理员登录}的请求,参数{}",adminLoginDTO);
        String jwt = iAdminService.login(adminLoginDTO);
        return JsonResult.ok(jwt);
    }


    @ApiOperation("添加管理员")
    @PreAuthorize("hasAuthority('/ams/admin/add-new')")
    @ApiOperationSupport(order = 100)
    @PostMapping("add-new")
    public JsonResult<Void> addNew(AdminAddNewDTO adminAddNewDTO){
        iAdminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }


    @GetMapping("")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @ApiOperation("查看管理员列表")
    public JsonResult<List<AdminListItemVO>> list(
            @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal
            ){
        log.debug("开始处理查看管理员列表请求");
        log.debug("当前登录当事人 :{}",loginPrincipal);
        List<AdminListItemVO> list = iAdminService.list();
        return JsonResult.ok(list);
    }


    @ApiOperationSupport(order = 300)
    @ApiOperation("根据id删除管理员")
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    @ApiImplicitParam(name = "id",value = "管理员id", required = true,dataType ="long" )
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1,message = "删除管理员失败,试删除的管理员的ID无效")
            @PathVariable Long id){
        log.debug("开始处理删除管理员请求");
        iAdminService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 400)
    @ApiOperation("根据id启用管理员")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiImplicitParam(name = "id",value = "管理员id", required = true,dataType ="long" )
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> Enable(@Range(min = 1,message = "管理员启用失败,启用的管理员的ID无效")
                                   @PathVariable Long id){
        log.debug("开始处里启用管理员请求");
        iAdminService.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 410)
    @ApiOperation("根据id禁用管理员")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiImplicitParam(name = "id",value = "管理员id", required = true,dataType ="long" )
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> Disable(@Range(min = 1,message = "管理员禁用失败,禁用的管理员的ID无效")
                                     @PathVariable Long id){
        log.debug("开始处里禁用管理员请求");
        iAdminService.setDisable(id);
        return JsonResult.ok();
    }



}
